// ranking.js
var iconUrl = require('../../config.js').iconUrl;
var net=require('../../utils/net.js');
//判断为空
function is_null(data) {
  if (data == 'undefined' || data == null || data == '') {
    return 0;
  } else {
    return data;
  }
}
Page({
  rangkingme:function(){
    var sort=this.data.me.sort;
    var zan=this.data.me.zan;
    var icon = this.data.me.icon;
    var sport_carorie = this.data.me.sport_carorie;
      wx.navigateTo({
        url: '/pages/rankingme/rankingme?sort=' + sort + "&zan=" + zan + "&sport_carorie" + sport_carorie+ "&icon=" + icon 
      });
  },
  /**
   * 页面的初始数据
   */
  data: {
    iconUrl:iconUrl,
    ranking_list: '',
    me:{
        nick_name:"",
        icon:"https://zwzlcloud.ocmcom.com/data/smallapp/icon/NO.3.png",
        zan:0,
        bracelet_id:0,
        sort:0,        
        sport_carorie: 0,        
    },
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.onPullDownRefresh();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {    
    var that=this;  
       var userinfo=wx.getStorageSync("userinfo");
       var datas={
          "bracelet_id":userinfo.bracelet_id,
          "pace": wx.getStorageSync("bracelet_data.current_pace"),
          "mileage": wx.getStorageSync("bracelet_data.current_mileage"),
          "sport_carorie": wx.getStorageSync("bracelet_data.current_calorie"),
          "sync_data": parseInt(new Date().getTime()/1000),
          "max_heart":wx.getStorageSync("bracelet_data.max_heart"),
          "min_heart": wx.getStorageSync("bracelet_data.min_heart"),
          "sport_consuming": wx.getStorageSync("bracelet_data.sport_consuming"),
          "shop_id": userinfo.shop_id
       }
    net.upload_data(datas);//上传数据
    setTimeout(function(){
    net.ranking_list(function (data) {
      if (data.flag) {        
        var ranking_list = data.data;
        that.setData({
          ranking_list: ranking_list
        });        
        var bracelet_id = is_null(wx.getStorageSync("userinfo").bracelet_id);
        ranking_list.forEach(
          function(obj){
            console.log(obj);
            if (obj.bracelet_id == bracelet_id) {              
              obj.sport_carorie = wx.getStorageSync("bracelet_data.current_calorie");
              that.setData({
                me: obj
              });
              return;
            }
          }
        );
      } else {
        wx.showToast({
          title: data.msg,
        })
      }
     }
      );
    }, 100);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})